@echo off

set cur_path=%~dp0

SETLOCAL enabledelayedexpansion

:main
call :check_tool
call :generate_coverage %1
exit

:check_tool
for /f "tokens=1 delims=" %%i in ('plink -V') do set plink_result=%%i
if "%plink_result%"=="" (
    echo Not found plink,Please download putty or plink!
    echo https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
    call :quit
)
for /f "tokens=1 delims=" %%i in ('pscp -V') do set pscp_result=%%i
if "%pscp_result%"=="" (
    echo Not found plink,Please download putty or plink!
    echo https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
    call :quit
)
exit /B 0

:generate_coverage
if exist %cur_path%my.ini (
    set index=0
    for /f "tokens=1 delims=" %%i in (my.ini) do (
        set config[!index!]=%%i
        set /a index=!index!+1
    )
    set name=!config[0]:~0,-1!
    set host=!config[1]:~0,-1!
    set password=!config[2]:~0,-1!
) else (
    echo Please input username
    set /p name=
    echo !name! >> %cur_path%my.ini
    echo Please input hostname
    set /p host=
    echo !host! >> %cur_path%my.ini
    echo Please input password
    set /p password=
    echo !password! >> %cur_path%my.ini
)

for /f "tokens=1 delims=" %%i in ('plink %name%@%host% -pw %password% "mkdir -p /home/%name%/OpenHarmony/master/out/generate_coverage"') do echo %%i



for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r .\generate_coverage\accesstokenid_test.gcno %name%@%host%:/home/%name%/OpenHarmony/master/out/generate_coverage') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo accesstokenid_test.gcno send fail!
    call :quit
)
for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r .\generate_coverage\accesstokenid_test.gcda %name%@%host%:/home/%name%/OpenHarmony/master/out/generate_coverage') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo accesstokenid_test.gcda send fail!
    call :quit
)


(
echo set -e
echo CUR=`realpath ${BASH_SOURCE[0]}`
echo DIR=`dirname ${CUR}`
echo TOP=`realpath ${DIR}/../../`
echo CLANG_DIR=${TOP}/prebuilts/clang/ohos/linux-x86_64/llvm
echo if [ -e "${CLANG_DIR}" ]; then
echo     echo find llvm-cov gcov
echo fi
echo ${CLANG_DIR}/bin/llvm-cov gcov $@) > .\generate_coverage\gcov_dos.sh

(
echo CUR=`realpath ${BASH_SOURCE[0]}`
echo DIR=`dirname ${CUR}`
echo TOP=`realpath ${DIR}/../../`
echo HOST_OUT=${TOP}/out/generate_coverage/
echo TEST_OUT=${HOST_OUT}/tests/unittest/linuxkerneltest/coverage
echo mkdir -p ${TEST_OUT}

echo lcov -c -d ${HOST_OUT} -o ${TEST_OUT}/accesstokenid_coverage.info --gcov-tool ${DIR}/gcov.sh
echo if [ $? -ne 0 ]; then
echo     echo "Install lcov: sudo apt install lcov"
echo     exit -1
echo fi
echo genhtml --demangle-cpp --rc genhtml_hi_limit=70 --rc genhtml_med_limit=50  -o ${TEST_OUT}/html ${TEST_OUT}/accesstokenid_coverage.info
echo tar -czf  ${TEST_OUT}/html.tar.gz -C ${TEST_OUT} html ) > .\generate_coverage\lcov_dos.sh

for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r .\generate_coverage\gcov_dos.sh %name%@%host%:/home/%name%/OpenHarmony/master/out/generate_coverage') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo gcov.sh send fail!
    call :quit
)

for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r .\generate_coverage\lcov_dos.sh %name%@%host%:/home/%name%/OpenHarmony/master/out/generate_coverage') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo lcov.sh send fail!
    call :quit
)

plink %name%@%host% -pw %password% "cd /home/%name%/OpenHarmony/master/out/generate_coverage;sed -e 's/.$//' gcov_dos.sh > gcov.sh; rm gcov_dos.sh; chmod 755 gcov.sh"
del .\generate_coverage\gcov_dos.sh

plink %name%@%host% -pw %password% "cd /home/%name%/OpenHarmony/master/out/generate_coverage;sed -e 's/.$//' lcov_dos.sh > lcov.sh; rm lcov_dos.sh; chmod 755 lcov.sh"
del .\generate_coverage\lcov_dos.sh

for /f "tokens=1 delims=" %%i in ('plink %name%@%host% -pw %password% sh /home/%name%/OpenHarmony/master/out/generate_coverage/lcov.sh') do echo %%i

for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r %name%@%host%:/home/%name%/OpenHarmony/master/out/generate_coverage/tests/unittest/linuxkerneltest/coverage/html.tar.gz .\generate_coverage') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo lcov.sh send fail!
    call :quit
)

exit /B 0

:quit
pause
exit
goto :EOF